{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 14.3 项目实施"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'message': 'ok',\n",
       " 'results': [{'address': '北京市海淀区新建宫门路19号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '2239',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=2a7a25ecf9cf13636d3e1bad&output=html&source=placeapi_v2',\n",
       "    'image_num': '219',\n",
       "    'overall_rating': '3.8',\n",
       "    'price': '30',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.998475, 'lng': 116.27487},\n",
       "   'name': '颐和园',\n",
       "   'street_id': '2a7a25ecf9cf13636d3e1bad',\n",
       "   'telephone': '010-62881144',\n",
       "   'uid': '2a7a25ecf9cf13636d3e1bad'},\n",
       "  {'address': '北京市朝阳区北辰东路15号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '10',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=451614f6ebe67020843304a2&output=html&source=placeapi_v2',\n",
       "    'image_num': '158',\n",
       "    'overall_rating': '4',\n",
       "    'price': '30',\n",
       "    'tag': '旅游景点;公园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.999658, 'lng': 116.402199},\n",
       "   'name': '北京奥林匹克公园',\n",
       "   'street_id': '451614f6ebe67020843304a2',\n",
       "   'telephone': '(010)84992008',\n",
       "   'uid': '451614f6ebe67020843304a2'},\n",
       "  {'address': '北京市海淀区西三环中路10号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '263',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=3810c87916332c8b9d09f7b2&output=html&source=placeapi_v2',\n",
       "    'image_num': '112',\n",
       "    'overall_rating': '3.3',\n",
       "    'price': '60',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.922649, 'lng': 116.326379},\n",
       "   'name': '玉渊潭公园',\n",
       "   'street_id': '3810c87916332c8b9d09f7b2',\n",
       "   'telephone': '(010)88653800',\n",
       "   'uid': '3810c87916332c8b9d09f7b2'},\n",
       "  {'address': '北京市西城区文津街1号(故宫北)',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '848',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=49d959381b59a043c61e5102&output=html&source=placeapi_v2',\n",
       "    'image_num': '500',\n",
       "    'overall_rating': '3.2',\n",
       "    'price': '5',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.932909, 'lng': 116.39548},\n",
       "   'name': '北海公园',\n",
       "   'street_id': '49d959381b59a043c61e5102',\n",
       "   'telephone': '(010)64040610',\n",
       "   'uid': '49d959381b59a043c61e5102'},\n",
       "  {'address': '北京市朝阳区朝阳公园南路1号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '109',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=8c3adf1c6f135ad74d554726&output=html&source=placeapi_v2',\n",
       "    'image_num': '108',\n",
       "    'overall_rating': '4.4',\n",
       "    'price': '50',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.950229, 'lng': 116.488036},\n",
       "   'name': '朝阳公园',\n",
       "   'street_id': '8c3adf1c6f135ad74d554726',\n",
       "   'uid': '8c3adf1c6f135ad74d554726'},\n",
       "  {'address': '北京市西城区羊房胡同',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '442',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=c21db9e5111538fe00ff2d32&output=html&source=placeapi_v2',\n",
       "    'image_num': '140',\n",
       "    'overall_rating': '4.8',\n",
       "    'price': '64',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.948094, 'lng': 116.391673},\n",
       "   'name': '什刹海公园',\n",
       "   'street_id': 'c21db9e5111538fe00ff2d32',\n",
       "   'uid': 'c21db9e5111538fe00ff2d32'},\n",
       "  {'address': '北京市西城区太平街19号(护城河北岸)',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '489',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=fa01e9378b06f14a774ff1f0&output=html&source=placeapi_v2',\n",
       "    'groupon_num': '1',\n",
       "    'image_num': '318',\n",
       "    'overall_rating': '5',\n",
       "    'price': '20',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.880237, 'lng': 116.388222},\n",
       "   'name': '陶然亭公园',\n",
       "   'street_id': 'fa01e9378b06f14a774ff1f0',\n",
       "   'telephone': '010-63588801,63532385',\n",
       "   'uid': 'fa01e9378b06f14a774ff1f0'},\n",
       "  {'address': '北京市海淀区香山卧佛寺路',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '372',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=550fd5f9c8d53478931fc9c4&output=html&source=placeapi_v2',\n",
       "    'image_num': '500',\n",
       "    'overall_rating': '5',\n",
       "    'tag': '旅游景点;植物园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 40.008633, 'lng': 116.216315},\n",
       "   'name': '北京植物园',\n",
       "   'street_id': '550fd5f9c8d53478931fc9c4',\n",
       "   'telephone': '(010)82598771',\n",
       "   'uid': '550fd5f9c8d53478931fc9c4'},\n",
       "  {'address': '北京市石景山区八大处路3号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '251',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=4edf9fbf7699998dcc06a44d&output=html&source=placeapi_v2',\n",
       "    'image_num': '351',\n",
       "    'overall_rating': '4.4',\n",
       "    'price': '10',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.960973, 'lng': 116.192635},\n",
       "   'name': '八大处公园',\n",
       "   'street_id': '4edf9fbf7699998dcc06a44d',\n",
       "   'uid': '4edf9fbf7699998dcc06a44d'},\n",
       "  {'address': '北京市海淀区中关村南大街35号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '150',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=49c6ce91c412a12c8d772700&output=html&source=placeapi_v2',\n",
       "    'image_num': '221',\n",
       "    'overall_rating': '4.7',\n",
       "    'price': '50',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.948412, 'lng': 116.32471},\n",
       "   'name': '紫竹院公园',\n",
       "   'street_id': '49c6ce91c412a12c8d772700',\n",
       "   'telephone': '010-68425851,88412800',\n",
       "   'uid': '49c6ce91c412a12c8d772700'},\n",
       "  {'address': '北京市海淀区清华西路28号圆明园遗址公园内',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '1311',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=62d852adf09e449a2fb17ef5&output=html&source=placeapi_v2',\n",
       "    'image_num': '265',\n",
       "    'overall_rating': '3.6',\n",
       "    'price': '15',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 40.018229, 'lng': 116.30498},\n",
       "   'name': '圆明园遗址公园',\n",
       "   'street_id': '62d852adf09e449a2fb17ef5',\n",
       "   'telephone': '010-62543673,010-62628501,010-62637561',\n",
       "   'uid': '62d852adf09e449a2fb17ef5'},\n",
       "  {'address': '丰台区长辛店镇大灰厂东路55号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '75',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=f60a15ed8f5278ecbb17e704&output=html&source=placeapi_v2',\n",
       "    'image_num': '127',\n",
       "    'overall_rating': '3.9',\n",
       "    'price': '10',\n",
       "    'tag': '旅游景点;公园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.869091, 'lng': 116.127118},\n",
       "   'name': '北宫国家森林公园',\n",
       "   'street_id': 'f60a15ed8f5278ecbb17e704',\n",
       "   'telephone': '(010)83840830',\n",
       "   'uid': 'f60a15ed8f5278ecbb17e704'},\n",
       "  {'address': '东城区中华路4号(天安门西侧)',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '178',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=e2c734e489d8d15d15a190d1&output=html&source=placeapi_v2',\n",
       "    'image_num': '117',\n",
       "    'overall_rating': '4.4',\n",
       "    'price': '20',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.916702, 'lng': 116.400582},\n",
       "   'name': '中山公园',\n",
       "   'street_id': 'e2c734e489d8d15d15a190d1',\n",
       "   'telephone': '(010)66055431',\n",
       "   'uid': 'e2c734e489d8d15d15a190d1'},\n",
       "  {'address': '北京市崇文区崇文门东大街9号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '68',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=6ee01020b9c09aef52e5c5d1&output=html&source=placeapi_v2',\n",
       "    'image_num': '38',\n",
       "    'overall_rating': '4.2',\n",
       "    'price': '10',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.907267, 'lng': 116.433796},\n",
       "   'name': '明城墙遗址公园',\n",
       "   'street_id': '6ee01020b9c09aef52e5c5d1',\n",
       "   'telephone': '(010)65270574',\n",
       "   'uid': '6ee01020b9c09aef52e5c5d1'},\n",
       "  {'address': '王佐镇南宫温泉度假酒店西侧',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '43',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=9271a6024fc2257ed518b735&output=html&source=placeapi_v2',\n",
       "    'image_num': '26',\n",
       "    'overall_rating': '4.2',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.804322, 'lng': 116.149983},\n",
       "   'name': '南宫体育公园',\n",
       "   'street_id': '9271a6024fc2257ed518b735',\n",
       "   'uid': '9271a6024fc2257ed518b735'},\n",
       "  {'address': '京广线',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '155',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=e85e2527b6f7615395f08cf8&output=html&source=placeapi_v2',\n",
       "    'groupon_num': '7',\n",
       "    'image_num': '500',\n",
       "    'overall_rating': '3.8',\n",
       "    'price': '80',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.502581, 'lng': 116.334877},\n",
       "   'name': '北京野生动物园',\n",
       "   'street_id': 'e85e2527b6f7615395f08cf8',\n",
       "   'telephone': '(010)89216666',\n",
       "   'uid': 'e85e2527b6f7615395f08cf8'},\n",
       "  {'address': '北京市西城区西直门外大街137号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '708',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=4bb049d91598944799005000&output=html&source=placeapi_v2',\n",
       "    'image_num': '500',\n",
       "    'overall_rating': '3.2',\n",
       "    'price': '10',\n",
       "    'tag': '旅游景点;动物园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.947983, 'lng': 116.34331},\n",
       "   'name': '北京动物园',\n",
       "   'street_id': '4bb049d91598944799005000',\n",
       "   'telephone': '010-68390274',\n",
       "   'uid': '4bb049d91598944799005000'},\n",
       "  {'address': '北京市西城区松树街1号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '1894',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=7aea43b721dd95e17d29bda4&output=html&source=placeapi_v2',\n",
       "    'image_num': '233',\n",
       "    'overall_rating': '4.7',\n",
       "    'price': '40',\n",
       "    'tag': '旅游景点;公园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.948226, 'lng': 116.38888},\n",
       "   'name': '后海公园',\n",
       "   'street_id': '7aea43b721dd95e17d29bda4',\n",
       "   'uid': '7aea43b721dd95e17d29bda4'},\n",
       "  {'address': '北京市朝阳区科荟路33号',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '364',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=03d7e5971f3675483c9a5e9e&output=html&source=placeapi_v2',\n",
       "    'image_num': '168',\n",
       "    'overall_rating': '4.6',\n",
       "    'price': '6',\n",
       "    'tag': '旅游景点;风景区',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 40.02523, 'lng': 116.396797},\n",
       "   'name': '奥林匹克森林公园',\n",
       "   'street_id': '03d7e5971f3675483c9a5e9e',\n",
       "   'telephone': '010-64529009',\n",
       "   'uid': '03d7e5971f3675483c9a5e9e'},\n",
       "  {'address': '东城区龙潭路8号(近华普超市)',\n",
       "   'detail': 1,\n",
       "   'detail_info': {'comment_num': '105',\n",
       "    'detail_url': 'http://api.map.baidu.com/place/detail?uid=090059e354c3dd9114b54b97&output=html&source=placeapi_v2',\n",
       "    'image_num': '120',\n",
       "    'overall_rating': '4.5',\n",
       "    'price': '30',\n",
       "    'tag': '旅游景点;公园',\n",
       "    'type': 'scope'},\n",
       "   'location': {'lat': 39.884502, 'lng': 116.447234},\n",
       "   'name': '龙潭公园',\n",
       "   'street_id': '090059e354c3dd9114b54b97',\n",
       "   'telephone': '010-67144336',\n",
       "   'uid': '090059e354c3dd9114b54b97'}],\n",
       " 'status': 0,\n",
       " 'total': 400}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "getjson('北京市')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.3.1 获取所有拥有公园的城市"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "province_list = ['江苏省', '浙江省', '广东省', '福建省', '山东省', '河南省', '河北省', '四川省', '辽宁省', '云南省', \n",
    "                 '湖南省', '湖北省', '江西省', '安徽省', '山西省', '广西壮族自治区', '陕西省', '黑龙江省', '内蒙古自治区', \n",
    "                 '贵州省', '吉林省', '甘肃省', '新疆维吾尔自治区', '海南省', '宁夏回族自治区', '青海省', '西藏自治区']\n",
    "for eachprovince in province_list:\n",
    "    decodejson = getjson(eachprovince)\n",
    "    for eachcity in decodejson['results']:\n",
    "        city = eachcity['name']\n",
    "        num = eachcity['num']        \n",
    "        output = '\\t'.join([city, str(num)]) + '\\r\\n'\n",
    "        with open('cities.txt', \"a+\", encoding='utf-8') as f:\n",
    "            f.write(output)\n",
    "            f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "def getjson(loc):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': 0, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "decodejson = getjson('全国')\n",
    "six_cities_list = ['北京市','上海市','重庆市','天津市','香港特别行政区','澳门特别行政区',]\n",
    "for eachprovince in decodejson['results']:\n",
    "    city = eachprovince['name']\n",
    "    num = eachprovince['num']\n",
    "    if city in six_cities_list:\n",
    "        output = '\\t'.join([city, str(num)]) + '\\r\\n'\n",
    "        with open('cities.txt', \"a+\", encoding='utf-8') as f:\n",
    "            f.write(output)\n",
    "            f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 14.3.2 获取所有城市的公园数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "import MySQLdb\n",
    "\n",
    "conn= MySQLdb.connect(host='localhost' , user='root', passwd='root', db ='baidumap', charset=\"utf8\")\n",
    "cur = conn.cursor()\n",
    "sql = \"\"\"CREATE TABLE city (\n",
    "         id INT NOT NULL AUTO_INCREMENT,\n",
    "         city VARCHAR(200) NOT NULL,\n",
    "         park VARCHAR(200) NOT NULL,\n",
    "         location_lat FLOAT,\n",
    "         location_lng FLOAT,\n",
    "         address VARCHAR(200),\n",
    "         street_id VARCHAR(200),\n",
    "         uid VARCHAR(200),\n",
    "         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n",
    "         PRIMARY KEY (id)\n",
    "         );\"\"\"\n",
    "cur.execute(sql)\n",
    "cur.close()\n",
    "conn.commit()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "city_list = list()\n",
    "with open(\"cities.txt\", 'r', encoding='utf-8') as txt_file:   \n",
    "    for eachLine in txt_file:\n",
    "        if eachLine != \"\" and eachLine != \"\\n\":\n",
    "            fields = eachLine.split(\"\\t\")\n",
    "            city = fields[0]\n",
    "            city_list.append(city)\n",
    "    txt_file.close()   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "南京市 0\n",
      "南京市 1\n",
      "南京市 2\n",
      "南京市 3\n",
      "南京市 4\n",
      "南京市 5\n",
      "南京市 6\n",
      "南京市 7\n",
      "南京市 8\n",
      "南京市 9\n",
      "南京市 10\n",
      "南京市 11\n",
      "南京市 12\n",
      "南京市 13\n",
      "南京市 14\n",
      "南京市 15\n",
      "南京市 16\n",
      "苏州市 0\n",
      "苏州市 1\n",
      "苏州市 2\n",
      "苏州市 3\n",
      "苏州市 4\n",
      "苏州市 5\n",
      "苏州市 6\n",
      "苏州市 7\n",
      "苏州市 8\n",
      "苏州市 9\n",
      "苏州市 10\n",
      "苏州市 11\n",
      "苏州市 12\n",
      "苏州市 13\n",
      "苏州市 14\n",
      "苏州市 15\n",
      "苏州市 16\n",
      "苏州市 17\n",
      "苏州市 18\n",
      "无锡市 0\n",
      "无锡市 1\n",
      "无锡市 2\n",
      "无锡市 3\n",
      "无锡市 4\n",
      "无锡市 5\n",
      "无锡市 6\n",
      "无锡市 7\n",
      "无锡市 8\n",
      "无锡市 9\n",
      "无锡市 10\n",
      "无锡市 11\n",
      "无锡市 12\n",
      "常州市 0\n",
      "常州市 1\n",
      "常州市 2\n",
      "常州市 3\n",
      "常州市 4\n",
      "常州市 5\n",
      "常州市 6\n",
      "常州市 7\n",
      "常州市 8\n",
      "南通市 0\n",
      "南通市 1\n",
      "南通市 2\n",
      "南通市 3\n",
      "南通市 4\n",
      "南通市 5\n",
      "盐城市 0\n",
      "盐城市 1\n",
      "盐城市 2\n",
      "盐城市 3\n",
      "盐城市 4\n",
      "盐城市 5\n",
      "盐城市 6\n",
      "盐城市 7\n",
      "徐州市 0\n",
      "徐州市 1\n",
      "徐州市 2\n",
      "徐州市 3\n",
      "徐州市 4\n",
      "徐州市 5\n",
      "徐州市 6\n",
      "徐州市 7\n",
      "扬州市 0\n",
      "扬州市 1\n",
      "扬州市 2\n",
      "扬州市 3\n",
      "扬州市 4\n",
      "扬州市 5\n",
      "扬州市 6\n",
      "淮安市 0\n",
      "淮安市 1\n",
      "淮安市 2\n",
      "淮安市 3\n",
      "淮安市 4\n",
      "淮安市 5\n",
      "泰州市 0\n",
      "泰州市 1\n",
      "泰州市 2\n",
      "泰州市 3\n",
      "泰州市 4\n",
      "泰州市 5\n",
      "宿迁市 0\n",
      "宿迁市 1\n",
      "宿迁市 2\n",
      "宿迁市 3\n",
      "镇江市 0\n",
      "镇江市 1\n",
      "镇江市 2\n",
      "镇江市 3\n",
      "镇江市 4\n",
      "连云港市 0\n",
      "连云港市 1\n",
      "连云港市 2\n",
      "连云港市 3\n",
      "杭州市 0\n",
      "杭州市 1\n",
      "杭州市 2\n",
      "杭州市 3\n",
      "杭州市 4\n",
      "杭州市 5\n",
      "杭州市 6\n",
      "杭州市 7\n",
      "杭州市 8\n",
      "杭州市 9\n",
      "杭州市 10\n",
      "杭州市 11\n",
      "杭州市 12\n",
      "杭州市 13\n",
      "杭州市 14\n",
      "杭州市 15\n",
      "杭州市 16\n",
      "杭州市 17\n",
      "杭州市 18\n",
      "杭州市 19\n",
      "杭州市 20\n",
      "宁波市 0\n",
      "宁波市 1\n",
      "宁波市 2\n",
      "宁波市 3\n",
      "宁波市 4\n",
      "宁波市 5\n",
      "宁波市 6\n",
      "宁波市 7\n",
      "宁波市 8\n",
      "宁波市 9\n",
      "宁波市 10\n",
      "宁波市 11\n",
      "宁波市 12\n",
      "宁波市 13\n",
      "宁波市 14\n",
      "宁波市 15\n",
      "宁波市 16\n",
      "温州市 0\n",
      "温州市 1\n",
      "温州市 2\n",
      "温州市 3\n",
      "温州市 4\n",
      "温州市 5\n",
      "温州市 6\n",
      "温州市 7\n",
      "温州市 8\n",
      "温州市 9\n",
      "温州市 10\n",
      "温州市 11\n",
      "温州市 12\n",
      "温州市 13\n",
      "嘉兴市 0\n",
      "嘉兴市 1\n",
      "嘉兴市 2\n",
      "嘉兴市 3\n",
      "嘉兴市 4\n",
      "嘉兴市 5\n",
      "嘉兴市 6\n",
      "嘉兴市 7\n",
      "嘉兴市 8\n",
      "金华市 0\n",
      "金华市 1\n",
      "金华市 2\n",
      "金华市 3\n",
      "金华市 4\n",
      "金华市 5\n",
      "金华市 6\n",
      "金华市 7\n",
      "金华市 8\n",
      "金华市 9\n",
      "绍兴市 0\n",
      "绍兴市 1\n",
      "绍兴市 2\n",
      "绍兴市 3\n",
      "绍兴市 4\n",
      "绍兴市 5\n",
      "绍兴市 6\n",
      "绍兴市 7\n",
      "台州市 0\n",
      "台州市 1\n",
      "台州市 2\n",
      "台州市 3\n",
      "台州市 4\n",
      "台州市 5\n",
      "台州市 6\n",
      "台州市 7\n",
      "湖州市 0\n",
      "湖州市 1\n",
      "湖州市 2\n",
      "湖州市 3\n",
      "湖州市 4\n",
      "丽水市 0\n",
      "丽水市 1\n",
      "丽水市 2\n",
      "丽水市 3\n",
      "衢州市 0\n",
      "衢州市 1\n",
      "衢州市 2\n",
      "舟山市 0\n",
      "舟山市 1\n",
      "舟山市 2\n",
      "广州市 0\n",
      "广州市 1\n",
      "广州市 2\n",
      "广州市 3\n",
      "广州市 4\n",
      "广州市 5\n",
      "广州市 6\n",
      "广州市 7\n",
      "广州市 8\n",
      "广州市 9\n",
      "广州市 10\n",
      "广州市 11\n",
      "广州市 12\n",
      "广州市 13\n",
      "广州市 14\n",
      "广州市 15\n",
      "广州市 16\n",
      "广州市 17\n",
      "广州市 18\n",
      "广州市 19\n",
      "广州市 20\n",
      "深圳市 0\n",
      "深圳市 1\n",
      "深圳市 2\n",
      "深圳市 3\n",
      "深圳市 4\n",
      "深圳市 5\n",
      "深圳市 6\n",
      "深圳市 7\n",
      "深圳市 8\n",
      "深圳市 9\n",
      "深圳市 10\n",
      "深圳市 11\n",
      "深圳市 12\n",
      "深圳市 13\n",
      "深圳市 14\n",
      "深圳市 15\n",
      "深圳市 16\n",
      "深圳市 17\n",
      "深圳市 18\n",
      "深圳市 19\n",
      "深圳市 20\n",
      "东莞市 0\n",
      "东莞市 1\n",
      "东莞市 2\n",
      "东莞市 3\n",
      "东莞市 4\n",
      "东莞市 5\n",
      "东莞市 6\n",
      "东莞市 7\n",
      "东莞市 8\n",
      "东莞市 9\n",
      "东莞市 10\n",
      "东莞市 11\n",
      "东莞市 12\n",
      "东莞市 13\n",
      "东莞市 14\n",
      "佛山市 0\n",
      "佛山市 1\n",
      "佛山市 2\n",
      "佛山市 3\n",
      "佛山市 4\n",
      "佛山市 5\n",
      "佛山市 6\n",
      "佛山市 7\n",
      "佛山市 8\n",
      "佛山市 9\n",
      "佛山市 10\n",
      "佛山市 11\n",
      "佛山市 12\n",
      "佛山市 13\n",
      "佛山市 14\n",
      "佛山市 15\n",
      "佛山市 16\n",
      "佛山市 17\n",
      "佛山市 18\n",
      "佛山市 19\n",
      "佛山市 20\n",
      "江门市 0\n",
      "江门市 1\n",
      "江门市 2\n",
      "江门市 3\n",
      "江门市 4\n",
      "江门市 5\n",
      "江门市 6\n",
      "江门市 7\n",
      "江门市 8\n",
      "江门市 9\n",
      "中山市 0\n",
      "中山市 1\n",
      "中山市 2\n",
      "中山市 3\n",
      "中山市 4\n",
      "中山市 5\n",
      "中山市 6\n",
      "惠州市 0\n",
      "惠州市 1\n",
      "惠州市 2\n",
      "惠州市 3\n",
      "惠州市 4\n",
      "惠州市 5\n",
      "珠海市 0\n",
      "珠海市 1\n",
      "珠海市 2\n",
      "珠海市 3\n",
      "珠海市 4\n",
      "珠海市 5\n",
      "梅州市 0\n",
      "梅州市 1\n",
      "梅州市 2\n",
      "梅州市 3\n",
      "梅州市 4\n",
      "湛江市 0\n",
      "湛江市 1\n",
      "湛江市 2\n",
      "湛江市 3\n",
      "湛江市 4\n",
      "汕头市 0\n",
      "汕头市 1\n",
      "汕头市 2\n",
      "汕头市 3\n",
      "韶关市 0\n",
      "韶关市 1\n",
      "韶关市 2\n",
      "韶关市 3\n",
      "揭阳市 0\n",
      "揭阳市 1\n",
      "揭阳市 2\n",
      "揭阳市 3\n",
      "肇庆市 0\n",
      "肇庆市 1\n",
      "肇庆市 2\n",
      "肇庆市 3\n",
      "清远市 0\n",
      "清远市 1\n",
      "清远市 2\n",
      "茂名市 0\n",
      "茂名市 1\n",
      "茂名市 2\n",
      "阳江市 0\n",
      "阳江市 1\n",
      "阳江市 2\n",
      "河源市 0\n",
      "河源市 1\n",
      "河源市 2\n",
      "汕尾市 0\n",
      "汕尾市 1\n",
      "汕尾市 2\n",
      "潮州市 0\n",
      "潮州市 1\n",
      "云浮市 0\n",
      "云浮市 1\n",
      "福州市 0\n",
      "福州市 1\n",
      "福州市 2\n",
      "福州市 3\n",
      "福州市 4\n",
      "福州市 5\n",
      "福州市 6\n",
      "福州市 7\n",
      "福州市 8\n",
      "福州市 9\n",
      "福州市 10\n",
      "福州市 11\n",
      "福州市 12\n",
      "福州市 13\n",
      "福州市 14\n",
      "厦门市 0\n",
      "厦门市 1\n",
      "厦门市 2\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[1;32m    383\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.7, use buffering of HTTP responses\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 384\u001b[0;31m                 \u001b[0mhttplib_response\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbuffering\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    385\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.6 and older, Python 3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: getresponse() got an unexpected keyword argument 'buffering'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-b83491251333>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     28\u001b[0m     \u001b[0mpage_num\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     29\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[0mnot_last_page\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 30\u001b[0;31m         \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0meachcity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpage_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     31\u001b[0m         \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0meachcity\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpage_num\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     32\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'results'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-3-b83491251333>\u001b[0m in \u001b[0;36mgetjson\u001b[0;34m(loc, page_num)\u001b[0m\n\u001b[1;32m     20\u001b[0m         \u001b[1;34m'ak'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'DDtVK6HPruSSkqHRj5gTk0rc'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m     }\n\u001b[0;32m---> 22\u001b[0;31m     \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"http://api.map.baidu.com/place/v2/search\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpa\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     23\u001b[0m     \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     24\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(url, params, **kwargs)\u001b[0m\n\u001b[1;32m     68\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     69\u001b[0m     \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'allow_redirects'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m     \u001b[1;32mreturn\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'get'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     72\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(method, url, **kwargs)\u001b[0m\n\u001b[1;32m     54\u001b[0m     \u001b[1;31m# cases, and look like a memory leak in others.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0msessions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 56\u001b[0;31m         \u001b[1;32mreturn\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     57\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     58\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[1;32m    486\u001b[0m         }\n\u001b[1;32m    487\u001b[0m         \u001b[0msend_kwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 488\u001b[0;31m         \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    489\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    490\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m    607\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    608\u001b[0m         \u001b[1;31m# Send the request\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 609\u001b[0;31m         \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    610\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    611\u001b[0m         \u001b[1;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\adapters.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[1;32m    421\u001b[0m                     \u001b[0mdecode_content\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    422\u001b[0m                     \u001b[0mretries\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_retries\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 423\u001b[0;31m                     \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    424\u001b[0m                 )\n\u001b[1;32m    425\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[0;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)\u001b[0m\n\u001b[1;32m    592\u001b[0m                                                   \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout_obj\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    593\u001b[0m                                                   \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 594\u001b[0;31m                                                   chunked=chunked)\n\u001b[0m\u001b[1;32m    595\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    596\u001b[0m             \u001b[1;31m# If we're going to release the connection in ``finally:``, then\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[1;32m    385\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.6 and older, Python 3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    386\u001b[0m                 \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 387\u001b[0;31m                     \u001b[0mhttplib_response\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    388\u001b[0m                 \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    389\u001b[0m                     \u001b[1;31m# Remove the TypeError from the exception chain in Python 3;\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mgetresponse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m   1329\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1330\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1331\u001b[0;31m                 \u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbegin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1332\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1333\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mbegin\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    295\u001b[0m         \u001b[1;31m# read until we get a non-100 response\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    296\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 297\u001b[0;31m             \u001b[0mversion\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstatus\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreason\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    298\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mstatus\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mCONTINUE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    299\u001b[0m                 \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_read_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    256\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    257\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 258\u001b[0;31m         \u001b[0mline\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_MAXLINE\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"iso-8859-1\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    259\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0m_MAXLINE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    260\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mLineTooLong\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"status line\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\socket.py\u001b[0m in \u001b[0;36mreadinto\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m    584\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    585\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 586\u001b[0;31m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrecv_into\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    587\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    588\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_timeout_occurred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "import MySQLdb\n",
    "\n",
    "conn= MySQLdb.connect(host='localhost' , user='root', passwd='root', db ='baidumap', charset=\"utf8\")\n",
    "cur = conn.cursor()\n",
    "\n",
    "def getjson(loc,page_num):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'q': '公园', \n",
    "        'region': loc,\n",
    "        'scope': '2',\n",
    "        'page_size': 20,\n",
    "        'page_num': page_num, \n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/search\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "for eachcity in city_list:\n",
    "    not_last_page = True\n",
    "    page_num = 0\n",
    "    while not_last_page:\n",
    "        decodejson = getjson(eachcity, page_num)\n",
    "        print (eachcity, page_num)\n",
    "        if decodejson['results']: \n",
    "            for eachone in decodejson['results']:\n",
    "                try:\n",
    "                    park = eachone['name']\n",
    "                except:\n",
    "                    park = None\n",
    "                try:\n",
    "                    location_lat = eachone['location']['lat']\n",
    "                except:\n",
    "                    location_lat = None\n",
    "                try:\n",
    "                    location_lng = eachone['location']['lng']\n",
    "                except:\n",
    "                    location_lng = None            \n",
    "                try:\n",
    "                    address = eachone['address']\n",
    "                except:\n",
    "                    address = None              \n",
    "                try:\n",
    "                    street_id = eachone['street_id']\n",
    "                except:\n",
    "                    street_id = None            \n",
    "                try:\n",
    "                    uid = eachone['uid']\n",
    "                except:\n",
    "                    uid = None\n",
    "                sql = \"\"\"INSERT INTO baidumap.city\n",
    "                (city, park, location_lat, location_lng, address, street_id, uid)\n",
    "                VALUES\n",
    "                (%s, %s, %s, %s, %s, %s, %s);\"\"\"\n",
    "    \n",
    "                cur.execute(sql, (eachcity, park, location_lat, location_lng, address, street_id, uid,))\n",
    "                conn.commit()\n",
    "            page_num += 1\n",
    "        else:        \n",
    "            not_last_page = False  \n",
    "cur.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.3.3 获取所有公园的详细信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#coding=utf-8\n",
    "import MySQLdb\n",
    "\n",
    "conn= MySQLdb.connect(host='localhost' , user='root', passwd='root', db ='baidumap', charset=\"utf8\")\n",
    "cur = conn.cursor()\n",
    "sql = \"\"\"CREATE TABLE park (\n",
    "         id INT NOT NULL AUTO_INCREMENT,\n",
    "         park VARCHAR(200) NOT NULL,\n",
    "         location_lat FLOAT,\n",
    "         location_lng FLOAT,\n",
    "         address VARCHAR(200),\n",
    "         street_id VARCHAR(200),\n",
    "         telephone VARCHAR(200),\n",
    "         detail INT,\n",
    "         uid VARCHAR(200),\n",
    "         tag VARCHAR(200),\n",
    "         type VARCHAR(200),\n",
    "         detail_url VARCHAR(800),\n",
    "         price INT,\n",
    "         overall_rating FLOAT,\n",
    "         image_num INT,\n",
    "         comment_num INT,\n",
    "         shop_hours VARCHAR(800),\n",
    "         alias VARCHAR(800),\n",
    "         keyword VARCHAR(800),\n",
    "         scope_type VARCHAR(200),\n",
    "         scope_grade VARCHAR(200),\n",
    "         description VARCHAR(9000),\n",
    "         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n",
    "         PRIMARY KEY (id)\n",
    "         );\"\"\"\n",
    "cur.execute(sql)\n",
    "cur.close()\n",
    "conn.commit()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# coding: utf-8\n",
    "\n",
    "import requests\n",
    "import json\n",
    "import MySQLdb\n",
    "\n",
    "conn= MySQLdb.connect(host='localhost' , user='root', passwd='root', db ='baidumap', charset=\"utf8\")\n",
    "cur = conn.cursor()\n",
    "sql = \"Select uid from baidumap.city where id > 0;\"\n",
    "\n",
    "cur.execute(sql)\n",
    "conn.commit()\n",
    "results = cur.fetchall()\n",
    "cur.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6265d58ddc79ad61df1e5286\n",
      "f633606a1b34ba4e0b69d9fc\n",
      "03ff6e2ecd84c091bea24001\n",
      "c8b5cdefee5cd91a130daa75\n",
      "34e48508f7e105e2d9d891e0\n",
      "efd9d6d8cf721727ad31e57e\n",
      "e4e04e27a19a59fac6effb4d\n",
      "5211d21956d57d417dc87633\n",
      "22e11407a8d4a6add4b74ebd\n",
      "d52831f9e4362ba72ffaa7b7\n",
      "eef2d52832b1cfdb3be4a655\n",
      "623390999c0c8c5f740269eb\n",
      "04fe2d5163976352fe0eb070\n",
      "a826a1ab0fd025f32afc4877\n",
      "2cc3c8e85bd78cd64d2fdffc\n",
      "07f3c8b5361ca2636240a9ed\n",
      "dd6cf40292fb23273617dce0\n",
      "2ba05251cc18ec959d82ea7b\n",
      "be1ea5efb5665abf14948e7b\n",
      "08f1f1f5db98e0b488db7342\n",
      "87248d960f34b8b32806ce78\n",
      "a12baa4064085e55e856fa1f\n",
      "101bc61b20f637a3c42ba1ed\n",
      "8d8925ffcae762b23f9a5ec6\n",
      "3f826ed3e6c9b946d1f49eed\n",
      "2c0bd6c594aa68d742ab9aa1\n",
      "56f2f696a14675830aa8d41e\n",
      "7e4e4572265b301da2d2302f\n",
      "6136dc11f9f3ff7a7069d3a3\n",
      "80e2676727f4f821b21e36e0\n",
      "606ab0d0fc0d1a2a0e3ddae1\n",
      "ddf873b0aef3f78b40afe6f9\n",
      "3da907f36e318f50b320a81e\n",
      "0d574117ec59e459bd484261\n",
      "abef57be7f91f32a96f08cc2\n",
      "3f24425c3bb0b33cc16c1488\n",
      "b7021bf8d32519faf5e73be0\n",
      "6ed3083a05c1a5ca2a3a9fa1\n",
      "d5288966df674d979de3210d\n",
      "f7e43ddb2fc7b9433c9a5ed3\n",
      "d146b38881e0cfa375026982\n",
      "5b25f4468eedb0ea102935ef\n",
      "41392de9b700773bd6b74eb4\n",
      "1c6f01ab088eac43734b87b8\n",
      "f05537c6a02c0d67cd04ef60\n",
      "6c2dc4eaabc3d1826d20b9ca\n",
      "9f97b1c5846bce05b64f541e\n",
      "52e687ca6edc594475e5b6b6\n",
      "0c67f67c00ebf7112efaa792\n",
      "6bda52bf9a25ab02cc06a45a\n",
      "bdeab4c7a91540f50e3ddabe\n",
      "be1ea5efe7748ca614948e7d\n",
      "a6aedf8185d6b96ae215f939\n",
      "04bdf4a3c099ea4f344aaeb0\n",
      "a3e8341cb9c052954bfd3f86\n",
      "b1c556e85d3e090fb4265519\n",
      "8422e9dde8331633c17da279\n",
      "c6491f5595e0b3c7d0ff23a5\n",
      "96b672aa995d9975ce04ef56\n",
      "eb36750395b1a11800ec0119\n",
      "8fa14fc329c27313f7eafcc4\n",
      "f633606a1affbb4e0a69d9b1\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[1;32m    383\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.7, use buffering of HTTP responses\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 384\u001b[0;31m                 \u001b[0mhttplib_response\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbuffering\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    385\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.6 and older, Python 3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: getresponse() got an unexpected keyword argument 'buffering'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-16-1ea5e819cda7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mrow\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mresults\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     26\u001b[0m     \u001b[0muid\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrow\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 27\u001b[0;31m     \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgetjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     28\u001b[0m     \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0muid\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     29\u001b[0m     \u001b[0minfo\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-16-1ea5e819cda7>\u001b[0m in \u001b[0;36mgetjson\u001b[0;34m(uid)\u001b[0m\n\u001b[1;32m     19\u001b[0m         \u001b[1;34m'ak'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'DDtVK6HPruSSkqHRj5gTk0rc'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m     }\n\u001b[0;32m---> 21\u001b[0;31m     \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"http://api.map.baidu.com/place/v2/detail\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpa\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     22\u001b[0m     \u001b[0mdecodejson\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mdecodejson\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(url, params, **kwargs)\u001b[0m\n\u001b[1;32m     68\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     69\u001b[0m     \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'allow_redirects'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m     \u001b[1;32mreturn\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'get'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     72\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(method, url, **kwargs)\u001b[0m\n\u001b[1;32m     54\u001b[0m     \u001b[1;31m# cases, and look like a memory leak in others.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     55\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0msessions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 56\u001b[0;31m         \u001b[1;32mreturn\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     57\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     58\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36mrequest\u001b[0;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[1;32m    486\u001b[0m         }\n\u001b[1;32m    487\u001b[0m         \u001b[0msend_kwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 488\u001b[0;31m         \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    489\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    490\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, **kwargs)\u001b[0m\n\u001b[1;32m    607\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    608\u001b[0m         \u001b[1;31m# Send the request\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 609\u001b[0;31m         \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    610\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    611\u001b[0m         \u001b[1;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\adapters.py\u001b[0m in \u001b[0;36msend\u001b[0;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[1;32m    421\u001b[0m                     \u001b[0mdecode_content\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    422\u001b[0m                     \u001b[0mretries\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_retries\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 423\u001b[0;31m                     \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    424\u001b[0m                 )\n\u001b[1;32m    425\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[0;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)\u001b[0m\n\u001b[1;32m    592\u001b[0m                                                   \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout_obj\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    593\u001b[0m                                                   \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 594\u001b[0;31m                                                   chunked=chunked)\n\u001b[0m\u001b[1;32m    595\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    596\u001b[0m             \u001b[1;31m# If we're going to release the connection in ``finally:``, then\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\requests\\packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[1;32m    385\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# Python 2.6 and older, Python 3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    386\u001b[0m                 \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 387\u001b[0;31m                     \u001b[0mhttplib_response\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    388\u001b[0m                 \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    389\u001b[0m                     \u001b[1;31m# Remove the TypeError from the exception chain in Python 3;\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mgetresponse\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m   1329\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1330\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1331\u001b[0;31m                 \u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbegin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1332\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1333\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36mbegin\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    295\u001b[0m         \u001b[1;31m# read until we get a non-100 response\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    296\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 297\u001b[0;31m             \u001b[0mversion\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstatus\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreason\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    298\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mstatus\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[0mCONTINUE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    299\u001b[0m                 \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_read_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    256\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    257\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_read_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 258\u001b[0;31m         \u001b[0mline\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_MAXLINE\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"iso-8859-1\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    259\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0m_MAXLINE\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    260\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mLineTooLong\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"status line\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\ProgramData\\Anaconda3\\lib\\socket.py\u001b[0m in \u001b[0;36mreadinto\u001b[0;34m(self, b)\u001b[0m\n\u001b[1;32m    584\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    585\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 586\u001b[0;31m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrecv_into\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    587\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    588\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_timeout_occurred\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "import MySQLdb\n",
    "\n",
    "conn= MySQLdb.connect(host='localhost' , user='root', passwd='root', db ='baidumap', charset=\"utf8\")\n",
    "cur = conn.cursor()\n",
    "sql = \"Select uid from baidumap.city where id > 0;\"\n",
    "\n",
    "cur.execute(sql)\n",
    "conn.commit()\n",
    "results = cur.fetchall()\n",
    "\n",
    "def getjson(uid):\n",
    "    headers = {'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} \n",
    "    pa = {\n",
    "        'uid': uid, \n",
    "        'scope': '2',\n",
    "        'output': 'json',\n",
    "        'ak': 'DDtVK6HPruSSkqHRj5gTk0rc'\n",
    "    }\n",
    "    r = requests.get(\"http://api.map.baidu.com/place/v2/detail\", params=pa, headers= headers)\n",
    "    decodejson = json.loads(r.text)\n",
    "    return decodejson\n",
    "\n",
    "for row in results:\n",
    "    uid = row[0]\n",
    "    decodejson = getjson(uid)\n",
    "    print (uid)\n",
    "    info = decodejson['result']\n",
    "    try:\n",
    "        park = info['name']\n",
    "    except:\n",
    "        park = None\n",
    "    try:\n",
    "        location_lat = info['location']['lat']\n",
    "    except:\n",
    "        location_lat = None\n",
    "    try:\n",
    "        location_lng = info['location']['lng']\n",
    "    except:\n",
    "        location_lng = None            \n",
    "    try:\n",
    "        address = info['address']\n",
    "    except:\n",
    "        address = None              \n",
    "    try:\n",
    "        street_id = info['street_id']\n",
    "    except:\n",
    "        street_id = None\n",
    "    try:\n",
    "        telephone = info['telephone']\n",
    "    except:\n",
    "        telephone = None\n",
    "    try:\n",
    "        detail = info['detail']\n",
    "    except:\n",
    "        detail = None \n",
    "    try:\n",
    "        tag = info['detail_info']['tag']\n",
    "    except:\n",
    "        tag = None            \n",
    "    try:\n",
    "        detail_url = info['detail_info']['detail_url']\n",
    "    except:\n",
    "        detail_url = None\n",
    "    try:\n",
    "        type = info['detail_info']['type']\n",
    "    except:\n",
    "        type = None            \n",
    "    try:\n",
    "        overall_rating = info['detail_info']['overall_rating']\n",
    "    except:\n",
    "        overall_rating = None\n",
    "    try:\n",
    "        image_num = info['detail_info']['image_num']\n",
    "    except:\n",
    "        image_num = None\n",
    "    try:\n",
    "        comment_num = info['detail_info']['comment_num']\n",
    "    except:\n",
    "        comment_num = None\n",
    "    try:\n",
    "        key_words = ''\n",
    "        key_words_list = info['detail_info']['di_review_keyword']\n",
    "        for eachone in key_words_list:\n",
    "            key_words = key_words + eachone['keyword'] + '/'\n",
    "    except:\n",
    "        key_words = None\n",
    "    try:\n",
    "        shop_hours = info['detail_info']['shop_hours']\n",
    "    except:\n",
    "        shop_hours = None            \n",
    "    try:\n",
    "        alias = info['detail_info']['alias']\n",
    "    except:\n",
    "        alias = None\n",
    "    try:\n",
    "        scope_type = info['detail_info']['scope_type']\n",
    "    except:\n",
    "        scope_type = None            \n",
    "    try:\n",
    "        scope_grade = info['detail_info']['scope_grade']\n",
    "    except:\n",
    "        scope_grade = None\n",
    "    try:\n",
    "        description = info['detail_info']['description']\n",
    "    except:\n",
    "        description = None\n",
    "    sql = \"\"\"INSERT INTO baidumap.park\n",
    "    (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url, type, overall_rating, image_num, \n",
    "    comment_num, keyword, shop_hours, alias, scope_type, scope_grade, description)\n",
    "    VALUES\n",
    "    (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);\"\"\"\n",
    "\n",
    "    cur.execute(sql, (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url, \n",
    "                      type, overall_rating, image_num, comment_num, key_words, shop_hours, alias, scope_type, scope_grade, description,))\n",
    "    conn.commit()\n",
    "cur.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print (key_words)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
